package com.example.demo.study.beanLifcycle;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;



public class LifeCycleBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean {
    private String name;

    //实例化
    public LifeCycleBean() {
        System.out.println("1. 调用构造器：实例化Bean");
    }

   //属性注入
   //@Autowired
   //private XXX xxx;
    @Value("TestBean")
    public void setName(String name) {
        this.name = name;
        System.out.println("2. 设置属性：name=" + name);
    }

    //设置容器属性
    @Override
    public void setBeanName(String name) {
        System.out.println("3. BeanNameAware: " + name);
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        System.out.println("4. BeanFactoryAware");
    }
    @Override
    public void setApplicationContext(ApplicationContext ctx) {
        System.out.println("5. ApplicationContextAware");
    }

    //------------------------initialization start-----------

    //用来进行初始化的额外配置、比如初始化缓存、加载配置、建立连接等
    @PostConstruct
    public void postConstruct() {
        System.out.println("6. @PostConstruct方法");
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("7. InitializingBean.afterPropertiesSet()");
    }

    public void customInit() {
        System.out.println("8. 自定义init-method");
    }
    //------------------------initialization end-----------



    public void execute() {
        System.out.println("9. Bean业务方法执行");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("10. @PreDestroy方法");
    }

    @Override
    public void destroy() {
        System.out.println("11. DisposableBean.destroy()");
    }

    public void customDestroy() {
        System.out.println("12. 自定义destroy-method");
    }

}
